import time

import requests
import hashlib

def format_time(stamp=0, format_str='%Y-%m-%d'):
    l_time = time.localtime(stamp)
    time_str = time.strftime(format_str, l_time)
    return time_str

def get_md5(s):
    md = hashlib.md5()
    md.update(s.encode('utf-8'))
    return md.hexdigest()


host = 'http://testapi.cnfanews.com:8000/Response/Screen/Column.ashx?'
password = '23f464a6326543f28522a1242bb51047'

now_stamp = int(time.time())
now_date = format_time(now_stamp)
yesterday_date = format_time(now_stamp-86400)
nextday_date = format_time(now_stamp+86400)

url_dict = {
    "appid": "c98b32f906434099",  # 用户Key，填 c98b32f906434099
    "applicationid"	: 659,          # 用户ID，填659
    "columnid": 175858,  # columnid	栏目ID，填175858
    "count": 20,  # count	返回数量 如20
    "enddate": nextday_date,  # enddate	数据结束时间,如2019-12-5
    "orderby": "degree desc",                         # orderby	排序 degree desc(热度降序)
    "sign": "",                       #sign	MD5加密数据，加密的数据为：按url参数字母正排序的所有 参数值 追加，
                                    # 再加上客户密码（23f464a6326543f28522a1242bb51047）。必须为32位大写。
    "startdate": now_date,                       #startdate	数据开始时间,如 2019-12-1
    "time": now_stamp,  # time	当前时间戳,10位
    "type": 8,  # type	获取数据类型,填8
}
sign = ''
for k in url_dict:
    sign += str(url_dict[k])
sign += password

# md = hashlib.md5()
sign_hash = get_md5(sign).upper()
print(sign_hash)
url_dict['sign'] = sign_hash

print(url_dict)

for k in url_dict:
    host += k + '=' + str(url_dict[k]) + "&"

print(host)
res = requests.get(host)
print(res.text)
